package com.deli.easyexcel;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.deli.entity.WriteModel;
import com.deli.utils.FileUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: xudeli
 * @CreateDate: 2019/6/9 21:55
 **/

public class WriteExcel {

    @Test
    public void writeExcel() {
        try {
            //
            //BufferedOutputStream stream = cn.hutool.core.io.FileUtil.getOutputStream("xls/test.xls");

            //文件输出位置
            FileOutputStream fos = new FileOutputStream("xls/test.xls");
            //InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx");
            //获取xls流
            ExcelWriter writer = EasyExcelFactory.getWriter(fos);
            //创建一个sheet页的excel文件，
            Sheet sheet = new Sheet(1, 0, WriteModel.class);
            sheet.setSheetName("第一个sheet页");
            writer.write(createModeList(), sheet);
            //讲上下文中最终写入到指定的文件中
            writer.finish();
            //关闭流
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private List<WriteModel> createModeList() {
        List<WriteModel> writeModels = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            WriteModel writeModel = WriteModel.
                    builder().
                    name("" + i).
                    password("123456").
                    age(i).build();
            writeModels.add(writeModel);
        }
        return writeModels;
    }
}
